package com.lc.zpyh.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.SelectCanteenBySchoolIdApi;
import com.lc.zpyh.http.request.SelectSchoolByProvincesNameApi;
import com.lc.zpyh.http.response.SelectCanteenBySchoolBean;
import com.lc.zpyh.http.response.SelectSchoolByProvincesNameBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedbackSecondaryActivity extends AppActivity {
    @BindView(R.id.rv_school)
    RecyclerView rvSchool;
    @BindView(R.id.rv_food)
    RecyclerView rvFood;
//    item_feedback_select

    List<SelectCanteenBySchoolBean.ListBean> datafoodBeanList = new ArrayList<>();
    private BaseQuickAdapter<SelectCanteenBySchoolBean.ListBean, BaseViewHolder> foodadapter;
    List<SelectSchoolByProvincesNameBean.SchoolsBean> dataschoolBeanList = new ArrayList<>();
    private BaseQuickAdapter<SelectSchoolByProvincesNameBean.SchoolsBean, BaseViewHolder> schooladapter;

    private String school="";
    private String food="";
    private String schoolid="";
    private String foodid="";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedbacksecondary;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        schoolid= getIntent().getStringExtra("schoolId");
        school();

    }

    private void Food(String schoolid) {
        EasyHttp.post(this)
                .api(new SelectCanteenBySchoolIdApi().setSchoolid(schoolid)).request(new HttpCallback<SelectCanteenBySchoolBean>(this) {

            @Override
            public void onSucceed(SelectCanteenBySchoolBean data) {
                datafoodBeanList.clear();
                datafoodBeanList.addAll(data.getList());
                if (datafoodBeanList.size()>0) {

                    datafoodBeanList.get(0).setGrop(true);

                    if (foodadapter==null){


                    foodadapter = new BaseQuickAdapter<SelectCanteenBySchoolBean.ListBean,
                            BaseViewHolder>(R.layout.item_feedback_select, datafoodBeanList) {

                        @Override
                        protected void convert(BaseViewHolder childViewHolder, SelectCanteenBySchoolBean.ListBean bean) {
                            if (bean.isGrop()) {
                                childViewHolder.setBackgroundResource(R.id.tv_name, R.mipmap.bg_true_s);
                                food =bean.getName();
                                foodid =bean.getId() + "";

                            }else {
                                childViewHolder.setBackgroundResource(R.id.tv_name, R.drawable.bg_verification_code);
                            }
                            childViewHolder.setText(R.id.tv_name, bean.getName());
                        }
                    };
                    foodadapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                            for (SelectCanteenBySchoolBean.ListBean bean :datafoodBeanList){
                                bean.setGrop(false);
                            }
                            SelectCanteenBySchoolBean.ListBean bean = datafoodBeanList.get(position);
                                bean.setGrop(!bean.isGrop());
                                if (bean.isGrop()){
                                    food = datafoodBeanList.get(position).getName();
                                    foodid = datafoodBeanList.get(position).getId() + "";
                                }
                                foodadapter.notifyDataSetChanged();

                        }
                    });
                    rvFood.setAdapter(foodadapter);
                    }else {
                        foodadapter.notifyDataSetChanged();
                    }
                }else {


                }
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });


    }

    private void school() {
        EasyHttp.post(this)
                .api(new SelectSchoolByProvincesNameApi().setSchoolName("")).request(new HttpCallback<SelectSchoolByProvincesNameBean>(this) {

            private int pos;

            @Override
            public void onSucceed(SelectSchoolByProvincesNameBean data) {
                if (data.getCode()==0) {
                    dataschoolBeanList.clear();
                    dataschoolBeanList.addAll(data.getSchools());
                    if (dataschoolBeanList.size()>0) {
                        for (int i = 0; i <dataschoolBeanList.size() ; i++) {
                            if (String.valueOf(dataschoolBeanList.get(i).getSchoolId()).equals(schoolid)){
                                dataschoolBeanList.get(i).setGrop(true);
                                pos = i;
                            }else {
                                dataschoolBeanList.get(i).setGrop(false);
                            }
                        }

                        if (schooladapter==null) {
                            schooladapter = new BaseQuickAdapter<SelectSchoolByProvincesNameBean.SchoolsBean,
                                    BaseViewHolder>(R.layout.item_feedback_select, dataschoolBeanList) {

                                @Override
                                protected void convert(BaseViewHolder childViewHolder, SelectSchoolByProvincesNameBean.SchoolsBean bean) {
                                    childViewHolder.setText(R.id.tv_name, bean.getSchoolName());
                                    if (bean.isGrop()) {
                                        childViewHolder.setBackgroundResource(R.id.tv_name, R.mipmap.bg_true_s);
                                        Food(bean.getSchoolId() + "");
                                        school =bean.getSchoolName();
                                        schoolid =bean.getSchoolId() + "";
                                    }else {
                                        childViewHolder.setBackgroundResource(R.id.tv_name, R.drawable.bg_verification_code);
                                    }

                                }

                            };
                            schooladapter.setOnItemClickListener(new OnItemClickListener() {
                                @Override
                                public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                                    for (SelectSchoolByProvincesNameBean.SchoolsBean bean :dataschoolBeanList){
                                        bean.setGrop(false);
                                    }
                                    SelectSchoolByProvincesNameBean.SchoolsBean bean= dataschoolBeanList.get(position);
                                    bean.setGrop(!bean.isGrop());
                                    if (bean.isGrop()) {
                                        school = dataschoolBeanList.get(position).getSchoolName();
                                        schoolid = dataschoolBeanList.get(position).getSchoolId() + "";

                                    }
                                    schooladapter.notifyDataSetChanged();
                                }
                            });

                            rvSchool.setAdapter(schooladapter);
                            rvSchool.scrollToPosition(pos);

                        }else {
                            schooladapter.notifyDataSetChanged();
                        }
                        }
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });



    }

    @OnClick(R.id.btn_keep)
    public void onClick() {
        if (school.isEmpty()){
            ToastUtils.show("请选择学校");
            return;
        }
        if (food.isEmpty()){
            ToastUtils.show("请选择食堂");
            return;
        }
        Intent intent =getIntent();
        Bundle bundle =new Bundle();
        //传输的内容仍然是键值对的形式
        bundle.putString("school",school);
        bundle.putString("food",food);
        bundle.putString("schoolId",schoolid);
        bundle.putString("foodId",foodid);
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();
    }
}
